package com.leo.java.interpreter;

import java.util.StringTokenizer;

/**
 *
 * @author xuexiaolei
 * @version 2018年01月06日
 */
public class Context {
    private StringTokenizer tokenizer;//相当于js的split方法
    private String currentToken;

    public Context(String text){
        tokenizer = new StringTokenizer(text);
        nextToken();
    }

    public String nextToken(){
        if (tokenizer.hasMoreTokens()){
            currentToken = tokenizer.nextToken();
        }else {
            currentToken = null;
        }
        return currentToken;
    }

    public String currentToken(){
        return currentToken;
    }

    public void skipTOken(String token){
        if (!token.equals(currentToken)){
            System.err.println("错误提示：" + currentToken + "解释错误");
        }
        nextToken();
    }

    public int currentNumber(){
        int number = 0;
        try {
            number = Integer.parseInt(currentToken);
        }catch (NumberFormatException e){
            System.err.println("错误：" + e);
        }
        return number;
    }

}
